package com.okay.window

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.PixelFormat
import android.os.Build
import android.text.Layout
import android.util.DisplayMetrics
import android.view.*
import android.widget.LinearLayout
import android.widget.TextView
import androidx.lifecycle.LifecycleService
import com.okay.testclass.R
import org.greenrobot.eventbus.EventBus


/**
 *
 * 点击home键的时候   如果在上直播课的话，就弹出悬浮窗
 */
class TrtcWindowService : LifecycleService() {
    private lateinit var windowManager: WindowManager
    private var floatRootView: View? = null//悬浮窗View

    override fun onCreate() {
        super.onCreate()
        initObserve()
    }

    private fun initObserve() {
        ViewModleSuspend.apply {
            isVisible.observe(this@TrtcWindowService) {
                floatRootView?.visibility = if (it) View.VISIBLE else View.GONE
            }
            isShowSuspendWindow.observe(this@TrtcWindowService) {
                if (it) {
                    showWindow()
                } else {
                    dissWindow()
                }
            }
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun showWindow() {
        windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
        val outMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(outMetrics)
        var layoutParam = WindowManager.LayoutParams().apply {
            type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
            } else {
                WindowManager.LayoutParams.TYPE_PHONE
            }
            format = PixelFormat.RGBA_8888
            flags =WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            //位置大小设置
            width = WindowManager.LayoutParams.WRAP_CONTENT
            height = WindowManager.LayoutParams.WRAP_CONTENT
            gravity =  Gravity.BOTTOM
            //设置底部居中显示
            x = 0
            y =  0
        }
        // 新建悬浮窗控件
        floatRootView = LayoutInflater.from(this).inflate(R.layout.view_suspend, null)
        var leftLayout : LinearLayout? = floatRootView?.findViewById(R.id.suspend_layout_left)
        var rightLayout : LinearLayout? = floatRootView?.findViewById(R.id.suspend_layout_right)

        //返回直播课
        leftLayout?.setOnClickListener{

            val backToHome = Intent(applicationContext, WinActivity::class.java)
            backToHome.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            startActivity(backToHome)

        }
        //结束主讲
        rightLayout?.setOnClickListener{
            //通过eventbus将结束主讲通知发送给activity
            EventBus.getDefault().post(ViewModleSuspend.MessageEvent(1))
        }
        floatRootView?.setOnTouchListener(ItemViewTouchListener(layoutParam, windowManager))
        // 将悬浮窗控件添加到WindowManager
        windowManager.addView(floatRootView, layoutParam)
    }

    /**
     * 销毁悬浮窗
     */
    private fun dissWindow(){
        floatRootView?.let {
            floatRootView?.windowToken?.let {
                windowManager?.let {
                    windowManager?.removeView(floatRootView)
                }
            }
        }
    }

}